package com.ytjj.qmyx.supplychain.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.constants.BankerConstants;
import com.ytjj.qmyx.supplychain.common.model.request.FreigRequest;
import com.ytjj.qmyx.supplychain.common.model.request.YxAssignFreight;
import com.ytjj.qmyx.supplychain.common.model.request.YxAstrictRegion;
import com.ytjj.qmyx.supplychain.common.model.request.YxFreightTemplate;
import com.ytjj.qmyx.supplychain.common.model.response.ProductDataResponse;
import com.ytjj.qmyx.supplychain.common.model.response.YxFreightTemplateResponse;
import com.ytjj.qmyx.supplychain.mapper.FreightTemplateDao;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBanker;
import com.ytjj.qmyx.supplychain.service.Impl.FreightTemplateService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FreightTemplateServiceImpl implements FreightTemplateService {

    @Resource
    private FreightTemplateDao freightTemplateDao;

    @Resource
    private BankerService bankerService;

    @Override
    public CommonPage selectFreightTemplatePage(FreigRequest freigRequest) {
        PageHelper.startPage(freigRequest.getPageNum(), freigRequest.getPageSize());
        List<YxFreightTemplateResponse> list =freightTemplateDao.selectFreightTemplatePage(freigRequest.getTemplateName(),freigRequest.getBankerId());
        list.forEach(item -> {
            Integer id = item.getId();
            List<YxAssignFreight> yxAssignFreightList = freightTemplateDao.queryYxAssignFreightByFreightId(id);
            List<ProductDataResponse> productDataResponses = freightTemplateDao.queryProductDataResponse(id);
            item.setProductDataResponses(productDataResponses.size());
            item.setAssignFreightList(yxAssignFreightList);
        });
        return CommonPage.restPage(list);
    }

    @Override
    public Integer insertFreightTemplate(YxFreightTemplate yxFreightTemplate) {
        if(freightTemplateDao.selectFreightTemplatess(yxFreightTemplate.getTemplateName(),yxFreightTemplate.getBankerId())!=0){
            return 2;
        }
        if (null == yxFreightTemplate.getBankerId()){
            // 获取当前的登录人的id
            Integer bankerId = bankerService.getUserInfo().getId();
            yxFreightTemplate.setBankerId(bankerId);
        }
        freightTemplateDao.insertFreightTemplate(yxFreightTemplate);
        if(yxFreightTemplate.getAssignFreightList().size()!=0){
            freightTemplateDao.insertAssignFreightList(yxFreightTemplate.getAssignFreightList(), yxFreightTemplate.getId());
        }
        if(yxFreightTemplate.getAstrictRegion()!=null){
            freightTemplateDao.insertAstrictRegion(yxFreightTemplate.getAstrictRegion(),yxFreightTemplate.getId());
        }
        return 1;
    }

    @Override
    public YxFreightTemplateResponse selectFreightTemplateResponse(Integer id) {
        YxFreightTemplateResponse yxFreightTemplateResponse = freightTemplateDao.selectFreightTemplateResponse(id);
        yxFreightTemplateResponse.setAssignFreightList(freightTemplateDao.queryYxAssignFreightByFreightId(id));
        return yxFreightTemplateResponse;
    }

    @Override
    public Integer deleteFreightTemplate(Integer id) {
        freightTemplateDao.deleteFreightTemplate(id);
        freightTemplateDao.deleteAssignFreightList(id);
        freightTemplateDao.deleteAstrictRegionList(id);
        return 1;
    }

    @Override
    public Integer deleteAssignFreight(Integer id) {
        return freightTemplateDao.deleteAssignFreight(id);
    }

    @Override
    public Integer deleteAstrictRegion(Integer id) {
        return freightTemplateDao.deleteAstrictRegion(id);
    }

    @Override
    public Integer updataAssignFreight(YxAssignFreight yxAssignFreight) {
        return freightTemplateDao.updataAssignFreight(yxAssignFreight);
    }

    @Override
    public Integer updataAstrictRegion(YxAstrictRegion yxAstrictRegion) {
        return freightTemplateDao.updataAstrictRegion(yxAstrictRegion);
    }

    @Override
    public Integer updataFreightTemplate(YxFreightTemplate yxFreightTemplate) {
        freightTemplateDao.updataFreightTemplate(yxFreightTemplate);
        if(yxFreightTemplate.getFreightWay()==0 && yxFreightTemplate.getAssignFreightList().size()!=0) {
            freightTemplateDao.deleteAssignFreightList(yxFreightTemplate.getId());
            freightTemplateDao.insertAssignFreightList(yxFreightTemplate.getAssignFreightList(), yxFreightTemplate.getId());
        }

        if(CollectionUtils.isEmpty(yxFreightTemplate.getAssignFreightList())) {
            freightTemplateDao.deleteAssignFreightList(yxFreightTemplate.getId());
        }

        if(yxFreightTemplate.getAstrictRegion()==null){
            freightTemplateDao.deleteAstrictRegionList(yxFreightTemplate.getId());
        }else if(yxFreightTemplate.getAstrictRegion().getId()!=null){
            freightTemplateDao.updataAstrictRegion(yxFreightTemplate.getAstrictRegion());
        }else{
            freightTemplateDao.insertAstrictRegion(yxFreightTemplate.getAstrictRegion(),yxFreightTemplate.getId());
        }
        return 1;
    }

    @Override
    public Integer updateDfaults(Integer id) {
        freightTemplateDao.updataDefaults();
        freightTemplateDao.updataDefault(id);
        return 1;
    }
}
